/*
 * Gui_Draft.java
 *
 * Created on August 2, 2007, 4:54 PM
 * Copyright Administrator
 */
package form;

import com.avid.jaxb.PARAM;
import com.avid.jaxb.PARAMS;
import com.avid.jaxb.TESTTABLE;
import com.avid.jaxb.TestAndParams;
import com.sun.data.provider.impl.ListDataProvider;
import com.sun.data.provider.impl.ObjectListDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Body;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Checkbox;
import com.sun.webui.jsf.component.Form;
import com.sun.webui.jsf.component.Head;
import com.sun.webui.jsf.component.Html;
import com.sun.webui.jsf.component.Label;
import com.sun.webui.jsf.component.Link;
import com.sun.webui.jsf.component.MessageGroup;
import com.sun.webui.jsf.component.Page;
import com.sun.webui.jsf.component.PanelLayout;
import com.sun.webui.jsf.component.Tab;
import com.sun.webui.jsf.component.TabSet;
import com.sun.webui.jsf.component.Table;
import com.sun.webui.jsf.component.TableColumn;
import com.sun.webui.jsf.component.TableRowGroup;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.component.Upload;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import com.sun.webui.jsf.model.UploadedFile;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.ServletContext;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.sun.webui.jsf.component.StaticText;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class Gui_Draft extends AbstractPageBean {

    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
    private List<PARAM> paramList;
    private Table table = new Table();
    private TableRowGroup rowGroup = new TableRowGroup();
    private TableRowGroup rowGroup1 = new TableRowGroup();
    private MessageGroup messageGroup1 = new MessageGroup();
    String testName = null;
    String testDesc = null;
    String paramName = null;
    String paramType = null;
    String paramValue = null;
    String uploadedFileName = null;
    List ddownList = new ArrayList();

    TestAndParamsListDataProvider tpldp = new TestAndParamsListDataProvider();

    protected String getUploadedFileName() {
        String originalName = null;
        if (getXmlFileUploaded() != null){
                 originalName = getXmlFileUploaded().getOriginalName();
        }

        return originalName;
    }

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        listDataProvider1.setList(null);
    }
    private Page page1 = new Page();

    public Page getPage1() {
        return page1;
    }

    public void setPage1(Page p) {
        this.page1 = p;
    }
    private Html html1 = new Html();

    public Html getHtml1() {
        return html1;
    }

    public void setHtml1(Html h) {
        this.html1 = h;
    }
    private Head head1 = new Head();

    public Head getHead1() {
        return head1;
    }

    public void setHead1(Head h) {
        this.head1 = h;
    }
    private Link link1 = new Link();

    public Link getLink1() {
        return link1;
    }

    public void setLink1(Link l) {
        this.link1 = l;
    }
    private Body body1 = new Body();

    public Body getBody1() {
        return body1;
    }

    public void setBody1(Body b) {
        this.body1 = b;
    }
    private Form form1 = new Form();

    public Form getForm1() {
        return form1;
    }

    public void setForm1(Form f) {
        this.form1 = f;
    }
    private TabSet tabSet1 = new TabSet();

    public TabSet getTabSet1() {
        return tabSet1;
    }

    public void setTabSet1(TabSet ts) {
        this.tabSet1 = ts;
    }
    private Tab tab1 = new Tab();

    public Tab getTab1() {
        return tab1;
    }

    public void setTab1(Tab t) {
        this.tab1 = t;
    }
    private PanelLayout layoutPanel1 = new PanelLayout();

    public PanelLayout getLayoutPanel1() {
        return layoutPanel1;
    }

    public void setLayoutPanel1(PanelLayout pl) {
        this.layoutPanel1 = pl;
    }
    private Tab tab2 = new Tab();

    public Tab getTab2() {
        return tab2;
    }

    public void setTab2(Tab t) {
        this.tab2 = t;
    }
    private PanelLayout layoutPanel2 = new PanelLayout();

    public PanelLayout getLayoutPanel2() {
        return layoutPanel2;
    }

    public void setLayoutPanel2(PanelLayout pl) {
        this.layoutPanel2 = pl;
    }
    private Tab tab3 = new Tab();

    public Tab getTab3() {
        return tab3;
    }

    public void setTab3(Tab t) {
        this.tab3 = t;
    }
    private PanelLayout layoutPanel3 = new PanelLayout();

    public PanelLayout getLayoutPanel3() {
        return layoutPanel3;
    }

    public void setLayoutPanel3(PanelLayout pl) {
        this.layoutPanel3 = pl;
    }
    private Label chooseTestLabel = new Label();

    public Label getChooseTestLabel() {
        return chooseTestLabel;
    }

    public void setChooseTestLabel(Label l) {
        this.chooseTestLabel = l;
    }
    private Label chooseLabel = new Label();

    public Label getChooseLabel() {
        return chooseLabel;
    }

    public void setChooseLabel(Label l) {
        this.chooseLabel = l;
    }
    private Button newTestButton = new Button();

    public Button getNewTestButton() {
        return newTestButton;
    }

    public void setNewTestButton(Button b) {
        this.newTestButton = b;
    }
    private Checkbox checkbox1 = new Checkbox();

    public Checkbox getCheckbox1() {
        return checkbox1;
    }

    public void setCheckbox1(Checkbox c) {
        this.checkbox1 = c;
    }
    private Checkbox checkbox2 = new Checkbox();

    public Checkbox getCheckbox2() {
        return checkbox2;
    }

    public void setCheckbox2(Checkbox c) {
        this.checkbox2 = c;
    }
    private Upload xmlFileUpload = new Upload();

    public Upload getXmlFileUpload() {
        return xmlFileUpload;
    }

    public void setXmlFileUpload(Upload u) {
        this.xmlFileUpload = u;
    }
    private SingleSelectOptionsList dropDown1DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown1DefaultOptions() {
        return dropDown1DefaultOptions;
    }

    public void setDropDown1DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown1DefaultOptions = ssol;
    }
    private Button updateXMLMetaButton = new Button();

    public Button getUpdateXMLMetaButton() {
        return updateXMLMetaButton;
    }

    public void setUpdateXMLMetaButton(Button b) {
        this.updateXMLMetaButton = b;
    }
    private TextField iterationsTextField = new TextField();

    public TextField getIterationsTextField() {
        return iterationsTextField;
    }

    public void setIterationsTextField(TextField tf) {
        this.iterationsTextField = tf;
    }
    private ListDataProvider listDataProvider1 = new ListDataProvider();

    public ListDataProvider getListDataProvider1() {
        return listDataProvider1;
    }

    public void setListDataProvider1(ListDataProvider ldp) {
        this.listDataProvider1 = ldp;
    }
    private Table table1 = new Table();

    public Table getTable1() {
        return table1;
    }

    public void setTable1(Table t) {
        this.table1 = t;
    }
    private TableRowGroup tableRowGroup1 = new TableRowGroup();

    public TableRowGroup getTableRowGroup1() {
        return tableRowGroup1;
    }

    public void setTableRowGroup1(TableRowGroup trg) {
        this.tableRowGroup1 = trg;
    }
    private TableColumn tableColumn1 = new TableColumn();

    public TableColumn getTableColumn1() {
        return tableColumn1;
    }

    public void setTableColumn1(TableColumn tc) {
        this.tableColumn1 = tc;
    }
    private TextField textField4 = new TextField();

    public TextField getTextField4() {
        return textField4;
    }

    public void setTextField4(TextField tf) {
        this.textField4 = tf;
    }
    private TableColumn tableColumn2 = new TableColumn();

    public TableColumn getTableColumn2() {
        return tableColumn2;
    }

    public void setTableColumn2(TableColumn tc) {
        this.tableColumn2 = tc;
    }
    private TextField textField5 = new TextField();

    public TextField getTextField5() {
        return textField5;
    }

    public void setTextField5(TextField tf) {
        this.textField5 = tf;
    }
    private TableColumn tableColumn3 = new TableColumn();

    public TableColumn getTableColumn3() {
        return tableColumn3;
    }

    public void setTableColumn3(TableColumn tc) {
        this.tableColumn3 = tc;
    }
    private TextField textField6 = new TextField();

    public TextField getTextField6() {
        return textField6;
    }

    public void setTextField6(TextField tf) {
        this.textField6 = tf;
    }
    private ObjectListDataProvider objectListDataProvider1 = new ObjectListDataProvider();

    public ObjectListDataProvider getObjectListDataProvider1() {
        return objectListDataProvider1;
    }

    public void setObjectListDataProvider1(ObjectListDataProvider oldp) {
        this.objectListDataProvider1 = oldp;
    }
    private ObjectListDataProvider objectListDataProvider2 = new ObjectListDataProvider();

    public ObjectListDataProvider getObjectListDataProvider2() {
        return objectListDataProvider2;
    }

    public void setObjectListDataProvider2(ObjectListDataProvider oldp) {
        this.objectListDataProvider2 = oldp;
    }
    private Table table3 = new Table();

    public Table getTable3() {
        return table3;
    }

    public void setTable3(Table t) {
        this.table3 = t;
    }
    private TableRowGroup tableRowGroup3 = new TableRowGroup();

    public TableRowGroup getTableRowGroup3() {
        return tableRowGroup3;
    }

    public void setTableRowGroup3(TableRowGroup trg) {
        this.tableRowGroup3 = trg;
    }
    private Link link2 = new Link();

    public Link getLink2() {
        return link2;
    }

    public void setLink2(Link l) {
        this.link2 = l;
    }
    private TableColumn tableColumn7 = new TableColumn();

    public TableColumn getTableColumn7() {
        return tableColumn7;
    }

    public void setTableColumn7(TableColumn tc) {
        this.tableColumn7 = tc;
    }
    private StaticText staticText4 = new StaticText();

    public StaticText getStaticText4() {
        return staticText4;
    }

    public void setStaticText4(StaticText st) {
        this.staticText4 = st;
    }
    private TableColumn tableColumn8 = new TableColumn();

    public TableColumn getTableColumn8() {
        return tableColumn8;
    }

    public void setTableColumn8(TableColumn tc) {
        this.tableColumn8 = tc;
    }
    private StaticText staticText5 = new StaticText();

    public StaticText getStaticText5() {
        return staticText5;
    }

    public void setStaticText5(StaticText st) {
        this.staticText5 = st;
    }
    private TableColumn tableColumn9 = new TableColumn();

    public TableColumn getTableColumn9() {
        return tableColumn9;
    }

    public void setTableColumn9(TableColumn tc) {
        this.tableColumn9 = tc;
    }
    private TextField textField1 = new TextField();

    public TextField getTextField1() {
        return textField1;
    }

    public void setTextField1(TextField tf) {
        this.textField1 = tf;
    }
    private TableColumn tableColumn10 = new TableColumn();

    public TableColumn getTableColumn10() {
        return tableColumn10;
    }

    public void setTableColumn10(TableColumn tc) {
        this.tableColumn10 = tc;
    }
    private TableColumn tableColumn11 = new TableColumn();

    public TableColumn getTableColumn11() {
        return tableColumn11;
    }

    public void setTableColumn11(TableColumn tc) {
        this.tableColumn11 = tc;
    }
    private TextField textField3 = new TextField();

    public TextField getTextField3() {
        return textField3;
    }

    public void setTextField3(TextField tf) {
        this.textField3 = tf;
    }
    private MessageGroup messageGroup2 = new MessageGroup();

    public MessageGroup getMessageGroup2() {
        return messageGroup2;
    }

    public void setMessageGroup2(MessageGroup mg) {
        this.messageGroup2 = mg;
    }
    private Button updateXmlFileButton = new Button();

    public Button getUpdateXmlFileButton() {
        return updateXmlFileButton;
    }

    public void setUpdateXmlFileButton(Button b) {
        this.updateXmlFileButton = b;
    }
    private TextField textField2 = new TextField();

    public TextField getTextField2() {
        return textField2;
    }

    public void setTextField2(TextField tf) {
        this.textField2 = tf;
    }

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public Gui_Draft() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     *
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Gui_Draft Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {

        init();
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {

        ddownList.add("int");
        ddownList.add("String");
        ddownList.add("boolean");
        getSessionBean1().setDdList(ddownList);
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    public String tab1_action() {
        // TODO: Replace with your code
        return null;
    }

    public String tab2_action() {
        // TODO: Replace with your code
        return null;
    }

    public void xmlFileUpload_processValueChange(ValueChangeEvent event) {
        // TODO: Replace with your code
        uploadedFileName = getUploadedFileName();
        info("Uploaded file originally named '" + uploadedFileName + "' of size '" + getXmlFileUploaded().getSize() + "'");
        // Some browsers return complete path name, some don't
        // Make sure we only have the file name
        // First, try forward slash
        int index = uploadedFileName.lastIndexOf('/');
        String justFileName;
        if (index >= 0) {
            justFileName = uploadedFileName.substring(index + 1);
        } else {
            // Try backslash
            index = uploadedFileName.lastIndexOf('\\');
            if (index >= 0) {
                justFileName = uploadedFileName.substring(index + 1);
            } else {
                // No forward or back slashes
                justFileName = uploadedFileName;
            }
        }

        String uploadedFileType = getXmlFileUploaded().getContentType();

        if (uploadedFileType.equals("text/xml")) {
            try {
                // get the path to the C:/temp directory
                // from the servlet context
                ServletContext theApplicationsServletContext = (ServletContext) this.getExternalContext().getContext();

                File dir = new File("C:/temp");
                getSessionBean1().setPath(dir);

                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File file = new File(uploadedFileName);
                getSessionBean1().setXmlFile(file);
                getXmlFileUploaded().write(file);
                setParamTable_action();
            } catch (Exception ex) {
                error("Cannot upload file: " + justFileName);
            }
        }
    }

    public String setParamTable_action() throws JAXBException {

        uploadedFileName = getUploadedFileName();
        File file = new File(uploadedFileName); //dir.getCanonicalPath() + File.separatorChar + justFileName
        getSessionBean1().setXmlFile(file);

        File fileUpload = new File(uploadedFileName);
        fileUpload = new File(fileUpload.getName());

        TESTTABLE testTable = getTestTable();
        List tests = getTests(testTable);
        TESTTABLE.TEST test;

        @SuppressWarnings(value = "unchecked")
        List<TestAndParams> testAndParamsList = new ArrayList();

        PARAM param = new PARAM();

        Iterator itr = tests.iterator();
        while (itr.hasNext()) {

            test = (TESTTABLE.TEST) itr.next();

            testName = test.getName();
            testDesc = test.getDesc();
            PARAMS params = test.getPARAMS();
            paramList = params.getPARAM();

            Iterator paramItr = paramList.iterator();
            while (paramItr.hasNext()) {
                TestAndParams testAndParams = new TestAndParams("", "", "", "", "");
                param = (PARAM) paramItr.next();

                paramName = param.getName();
                paramType = param.getType();
                paramValue = param.getValue();

                testAndParams.setTestName(testName);
                testAndParams.setTestDesc(testDesc);
                testAndParams.setParamName(paramName);
                testAndParams.setParamType(paramType);
                testAndParams.setParamValue(paramValue);

                testAndParamsList.add(testAndParams);
            }
        }

        tpldp.setList(testAndParamsList);
        getSessionBean1().setTestAndParamsListDataProvider(tpldp);

        return "Properties";
    }

    protected UploadedFile getXmlFileUploaded() {

        UploadedFile uploadedFile = xmlFileUpload.getUploadedFile();
        return uploadedFile;
    }

    protected void setParamNameList(final List<String> paramNameList) {
        Iterator itr;
        itr = paramList.iterator();
        while (itr.hasNext()) {
            PARAM param = (PARAM) itr.next();
            paramNameList.add(param.getName());
        }
    }

    protected void setParamList(final List tests) {

        Iterator itr = tests.iterator();
        while (itr.hasNext()) {
            TESTTABLE.TEST test = (TESTTABLE.TEST) itr.next();

            PARAMS params = test.getPARAMS();
            paramList.addAll(params.getPARAM());
        }
    }

    protected List getTests(final TESTTABLE testTable) {
        List tests = testTable.getTESTSUITEOrTEST();
        return tests;
    }

    protected TESTTABLE getTestTable() throws JAXBException {

        JAXBContext jc = JAXBContext.newInstance("com.avid.jaxb");
        Unmarshaller unmarshaller = jc.createUnmarshaller();

        TESTTABLE testTable = (TESTTABLE) unmarshaller.unmarshal(getSessionBean1().getXmlFile());
        return testTable;
    }

    public String updateXmlFileButton_action() throws JAXBException{
        // TODO: Process the button click action. Return value is a navigation
        // case name where null will return to the same page.
//        tpldp.getObjects();
//        tpldp.commitChanges();
        
//                List<TestAndParams> testAndParamsList = new ArrayList();
//                PARAM param = new PARAM();
//                        TESTTABLE testTable = getTestTable();
//                        TESTTABLE.TEST test;
//            PARAMS params = test.getPARAMS();
//            paramList = params.getPARAM();
//
//            Iterator paramItr = paramList.iterator();
//            while (paramItr.hasNext()) {
//                TestAndParams testAndParams = new TestAndParams("", "", "", "", "");
//                param = (PARAM) paramItr.next();
//
//                paramName = param.getName();
//                paramType = param.getType();
//                paramValue = param.getValue();
//
//                testAndParams.getTestName();
//                testAndParams.getTestDesc();
//                testAndParams.getParamName();
//                testAndParams.getParamType();
//                testAndParams.getParamValue();
//
//                testAndParamsList.add(testAndParams);
//            }
//        
//
//        tpldp.setList(testAndParamsList);
//        getSessionBean1().setTestAndParamsListDataProvider(tpldp);
//        
        return null;
    }

    public String updateXMLMetaButton_action() {
        // TODO: Process the button click action. Return value is a navigation
        // case name where null will return to the same page.
        return null;
    }
}
